package com.jl.tools.ui.dialog;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.jl.tools.crypto.CryptoControl;
import com.jl.tools.crypto.CryptoFrm;
import com.jl.util.SWTDialogUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.wb.swt.ResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.ModifyEvent;

public class SecurityDialog extends Dialog implements CryptoFrm{

	protected Object result;
	protected Shell shell;
	private Text textKey;
	private Text textInput;
	private Text textOutput;
	private Combo comboPad;
	private Combo comboMode;
	private Combo comboAlg;
	private boolean isEncrypt;
	private Text textIV;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public SecurityDialog(Shell parent, int style) {
		super(parent, style);
		setText("Security Dialog");
		createContents();
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		Display display = getParent().getDisplay();
		SWTDialogUtil.locateToParentCenter(getParent(), shell);
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.MIN);
		shell.setImage(ResourceManager.getPluginImage("com.jl.smartcard.tools", "icons/security_16.png"));
		shell.setSize(627, 440);
		shell.setText(getText());
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		CTabFolder tabFolder = new CTabFolder(shell, SWT.BOTTOM);
		tabFolder.setSelectionBackground(Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
		
		CTabItem tbtmDesaes = new CTabItem(tabFolder, SWT.NONE);
		tbtmDesaes.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		tbtmDesaes.setText("DES/AES");
		
		Composite composite = new Composite(tabFolder, SWT.NONE);
		tbtmDesaes.setControl(composite);
		composite.setLayout(new GridLayout(1, false));
		
		Composite composite_1 = new Composite(composite, SWT.NONE);
		composite_1.setLayout(new FormLayout());
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		
		comboAlg = new Combo(composite_1, SWT.READ_ONLY);
		comboAlg.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				event_modify_combo_alg_text(e);
			}
		});
		comboAlg.setItems(new String[] {"DES", "AES"});
		comboAlg.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		FormData fd_comboAlg = new FormData();
		fd_comboAlg.right = new FormAttachment(0, 81);
		fd_comboAlg.top = new FormAttachment(0, 3);
		fd_comboAlg.left = new FormAttachment(0);
		comboAlg.setLayoutData(fd_comboAlg);
		
		comboMode = new Combo(composite_1, SWT.READ_ONLY);
		comboMode.setItems(new String[] {"ECB", "CBC"});
		comboMode.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		FormData fd_comboMode = new FormData();
		fd_comboMode.left = new FormAttachment(comboAlg, 6);
		fd_comboMode.top = new FormAttachment(comboAlg, 0, SWT.TOP);
		comboMode.setLayoutData(fd_comboMode);
		
		comboPad = new Combo(composite_1, SWT.READ_ONLY);
		comboPad.setItems(new String[] {"NoPadding", "ISO9797M2", "ISO9797M1", "PKCS5Padding"});
		fd_comboMode.right = new FormAttachment(100, -436);
		comboPad.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		FormData fd_comboPad = new FormData();
		fd_comboPad.left = new FormAttachment(comboMode, 6);
		fd_comboPad.top = new FormAttachment(comboAlg, 0, SWT.TOP);
		comboAlg.select(0);
		comboMode.select(0);
		comboPad.setLayoutData(fd_comboPad);
		
		Button btnEncrpt = new Button(composite_1, SWT.NONE);
		btnEncrpt.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				event_select_btn_encrypt();
			}
		});
		fd_comboPad.right = new FormAttachment(btnEncrpt, -155);
		comboPad.select(0);
		btnEncrpt.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		FormData fd_btnEncrpt = new FormData();
		fd_btnEncrpt.top = new FormAttachment(0, 1);
		btnEncrpt.setLayoutData(fd_btnEncrpt);
		btnEncrpt.setText("Encrypt");
		
		Button btnDecrypt = new Button(composite_1, SWT.NONE);
		btnDecrypt.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				event_select_btn_decrypt();
			}
		});
		fd_btnEncrpt.left = new FormAttachment(btnDecrypt, -81, SWT.LEFT);
		btnDecrypt.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		fd_btnEncrpt.right = new FormAttachment(btnDecrypt, -6);
		FormData fd_btnDecrypt = new FormData();
		fd_btnDecrypt.top = new FormAttachment(0, 1);
		fd_btnDecrypt.right = new FormAttachment(100);
		btnDecrypt.setLayoutData(fd_btnDecrypt);
		btnDecrypt.setText("Decrypt");
		
		textKey = new Text(composite, SWT.BORDER);
		textKey.setText("00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
		textKey.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		textKey.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		textIV = new Text(composite, SWT.BORDER);
		textIV.setText("00 00 00 00 00 00 00 00");
		textIV.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		GridData gd_textIV = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_textIV.widthHint = 109;
		textIV.setLayoutData(gd_textIV);
		
		textInput = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		textInput.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		GridData gd_textInput = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_textInput.heightHint = 130;
		textInput.setLayoutData(gd_textInput);
		
		textOutput = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		textOutput.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		GridData gd_textOutput = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
		gd_textOutput.heightHint = 133;
		textOutput.setLayoutData(gd_textOutput);
		
		CTabItem tbtmRsa = new CTabItem(tabFolder, SWT.NONE);
		tbtmRsa.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		tbtmRsa.setText("RSA");
		
		CTabItem tbtmSignature = new CTabItem(tabFolder, SWT.NONE);
		tbtmSignature.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		tbtmSignature.setText("Signature");
		
		CTabItem tbtmDigest = new CTabItem(tabFolder, SWT.NONE);
		tbtmDigest.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		tbtmDigest.setText("Digest");
		
		CTabItem tbtmChecksum = new CTabItem(tabFolder, SWT.NONE);
		tbtmChecksum.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		tbtmChecksum.setText("Checksum");
		
		CTabItem tbtmOther = new CTabItem(tabFolder, SWT.NONE);
		tbtmOther.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		tbtmOther.setText("Other");

	}

	protected void event_modify_combo_alg_text(ModifyEvent e) {
		String iv = CryptoControl.getDefaultIV(this.getAlg());
		if(this.textIV != null){
			this.textIV.setText(iv);
		}
		String[] pad = CryptoControl.getSupportPadMode(this.getAlg());
		if(this.comboPad != null){
			this.comboPad.setItems(pad);
			this.comboPad.select(1);
		}
	}

	protected void event_select_btn_decrypt() {
		isEncrypt = false;
		new CryptoControl(this).run();
	}

	protected void event_select_btn_encrypt() {
		isEncrypt = true;
		new CryptoControl(this).run();
	}

	@Override
	public String getAlg() {
		return this.comboAlg.getText();
	}

	@Override
	public String getPadding() {
		return this.comboPad.getText();
	}

	@Override
	public String getMode() {
		return this.comboMode.getText();
	}

	@Override
	public String getKey() {
		return this.textKey.getText();
	}

	@Override
	public String getInput() {
		return this.textInput.getText();
	}

	@Override
	public void setResult(String res) {
		this.textOutput.setText(res);
	}

	@Override
	public boolean isEncrpt() {
		return isEncrypt;
	}

	@Override
	public String getIV() {
		return this.textIV.getText();
	}
}
